Imports System.IO

Public Class WorkWithPath
  Private Sub BrowseButton_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles BrowseButton.Click

    ofd.Filter = _
       "Text files|*.txt|" & _
       "Database files|*.mdb;*.mda;*.mde|" & _
       "All files|*.*"

    ' These settings are optional, and are only 
    ' used here to demonstrate how you might 
    ' use them. 
    ofd.AddExtension = True
    ofd.CheckFileExists = True
    ofd.CheckPathExists = True
    ofd.DefaultExt = "txt"
    ' For historical reasons, this 
    ' index is 1-based.
    ofd.FilterIndex = 3
    ofd.InitialDirectory = "C:\"
    ofd.Multiselect = False
    ofd.ReadOnlyChecked = True
        ofd.RestoreDirectory = False
    ofd.ShowHelp = True
    ofd.ShowReadOnly = True
    ofd.SupportMultiDottedExtensions = True
    ofd.Title = "Select a File"
    ofd.ValidateNames = True

        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            PathNameTextBox.Text = ofd.FileName
        End If
  End Sub

  Private Sub RetrieveButton_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles RetrieveButton.Click

    Try
      HasExtensionCheckBox.Checked = _
       Path.HasExtension(PathNameTextBox.Text)
            IsPathRootedCheckBox.Checked = _
             Path.IsPathRooted(PathNameTextBox.Text)
      GetDirectoryNameLabel.Text = _
       Path.GetDirectoryName(PathNameTextBox.Text)
      GetExtensionLabel.Text = _
       Path.GetExtension(PathNameTextBox.Text)
      GetFileNameLabel.Text = _
       Path.GetFileName(PathNameTextBox.Text)
      GetFilenameWithExtensionLabel.Text = _
       Path.GetFileNameWithoutExtension(PathNameTextBox.Text)
      GetFullPathLabel.Text = _
       Path.GetFullPath(PathNameTextBox.Text)
      GetPathRootLabel.Text = _
       Path.GetPathRoot(PathNameTextBox.Text)

      GetTempFileNameLabel.Text = _
       Path.GetTempFileName()
      GetTempPathLabel.Text = _
       Path.GetTempPath()
      GetRandomFileNameLabel.Text = _
       Path.GetRandomFileName()
            GetInvalidFileNameCharsLabel.Text = _
       Path.GetInvalidFileNameChars()
      GetInvalidPathCharsLabel.Text = _
       Path.GetInvalidPathChars()

    Catch ex As Exception
      MessageBox.Show(ex.Message, "Error")
    End Try
  End Sub

  Private Sub WorkWithPath_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    RetrieveButton.Enabled = False
  End Sub

  Private Sub PathNameTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PathNameTextBox.TextChanged
    RetrieveButton.Enabled = (PathNameTextBox.Text.Length > 0)
  End Sub

  Private Sub ofd_FileOk( _
   ByVal sender As System.Object, _
   ByVal e As System.ComponentModel.CancelEventArgs) _
   Handles ofd.FileOk

    ' Demonstrate the FileOk event of the 
    ' the OpenFileDialog class. You wouldn't 
    ' normally include code like this.

    If MessageBox.Show( _
     "Do you really want to select " & ofd.FileName & "?", _
     "Confirm Selection", _
     MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
      e.Cancel = True
    End If
  End Sub

  Private Sub ofd_HelpRequest( _
   ByVal sender As Object, _
   ByVal e As System.EventArgs) _
   Handles ofd.HelpRequest

    MessageBox.Show("No help available.", _
     "Select File Help")
  End Sub

  Private Sub ResetCheckBox(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HasExtensionCheckBox.Click, IsPathRootedCheckBox.Click
    ' Put the value back the way it was. Because the control
    ' Doesn't have a "ReadOnly" property, this takes
    ' the place of making the control read only.
    Dim chkBox As CheckBox = CType(sender, CheckBox)
    chkBox.Checked = Not chkBox.Checked
  End Sub

End Class
